var e = require("../../@babel/runtime/helpers/interopRequireDefault").default;

Object.defineProperty(exports, "__esModule", {
    value: !0
}), exports.default = void 0;

var t = require("../../@babel/runtime/helpers/classCallCheck"), s = require("../../@babel/runtime/helpers/createClass"), c = e(require("./subscription")), r = e(require("./cache")), i = function() {
    function e() {
        var s;
        t(this, e), this.cache = new r.default(), this.getAutoIncrese = (s = 1, function() {
            return s++;
        });
    }
    return s(e, [ {
        key: "on",
        value: function(e, t, s) {
            if ("function" == typeof t) {
                this._stores = this._stores || {}, this.cache.cache[e] && this.cache.cache[e].forEach(function(e) {
                    t.apply(s, [ e ]);
                });
                var r = this.getAutoIncrese();
                return (this._stores[e] = this._stores[e] || []).push({
                    cbId: r,
                    cb: t,
                    ctx: s
                }), new c.default(this, e, r);
            }
            console.error("fn must be a function");
        }
    }, {
        key: "emit",
        value: function(e, t) {
            this.cache.cache[e] || (this.cache.cache[e] = []), this.cache.cache[e].length >= this.cache.cacheMax && this.cache.cache[e].shift(), 
            this.cache.cache[e].push(t), this._stores = this._stores || {};
            var s = this._stores[e];
            if (s) for (var c = 0, r = (s = s.slice(0)).length; c < r; c++) s[c].cb.apply(s[c].ctx, [ t ]);
        }
    }, {
        key: "off",
        value: function(e, t) {
            if (this._stores = this._stores || {}, arguments.length) {
                var s = this._stores[e];
                if (s) if (1 !== arguments.length) {
                    for (var c = 0, r = s.length; c < r; c++) if (s[c].cbId === t) {
                        s.splice(c, 1);
                        break;
                    }
                } else delete this._stores[e];
            } else this._stores = {};
        }
    }, {
        key: "offCache",
        value: function(e) {
            var t = this.cache;
            t && t[e] && delete t[e];
        }
    } ]), e;
}();

exports.default = i;